package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;

import java.util.List;

public class VideoPlayerMenuAdapter extends ArrayAdapter {
    private List<PlayMenuTvVo> items;
    private Context context;
    private Integer position;

    public VideoPlayerMenuAdapter(@NonNull Context context, List<PlayMenuTvVo> items) {
        super(context, 0);
        this.context = context;
        this.items = items;
        this.position = 0;
    }

    public void addAll(List<PlayMenuTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        if(this.items==null||this.items.size()<1){
            this.items.addAll(items);
        }else {
            this.items.clear();
            this.items.addAll(items);
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }
    public void setPosition(Integer position) {
        if (position < 0 || position > items.size()) {
            this.position = 0;
        } else {
            this.position = position;
        }
        notifyDataSetChanged();
    }
    public int getPosition() {
        if (position < 0 || position > items.size()) {
            return 0;
        }
        return position;
    }
    public List<PlayMenuTvVo> getItems() {
        if (items == null || items.size() < 1) {
            return null;
        }
        return items;
    }
    @Override
    public PlayMenuTvVo getItem(int position) {
        if (position < 0 || position > items.size()) {
            return null;
        }
        return items.get(position);
    }
    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position > items.size()) {
            return -1;
        } else if (position == this.position) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    class ViewHolder {
        TextView title;
    }

    private View getCreateView(ViewGroup viewGroup){
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        return layoutInflater.inflate(R.layout.view_item_video_menu, viewGroup, false);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        PlayMenuTvVo news=items.get(position);
        if(convertView==null){
            convertView=getCreateView(parent);
            ViewHolder holder=new ViewHolder();
            if(convertView!=null){
                holder.title=convertView.findViewById(R.id.title);
                convertView.setTag(holder);
            }
        }
        if(convertView!=null){
            final ViewHolder holder=(ViewHolder)convertView.getTag();
            if(holder.title!=null){
                holder.title.setText(news.getName());
                if(this.position==position){
                    holder.title.setTextColor(ContextCompat.getColor(context,R.color.main_color));
                }else {
                    holder.title.setTextColor(ContextCompat.getColor(context,R.color.black));
                }
            }
        }
        return convertView;
    }
}
